package com.epam.airlines;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import com.epam.factories.AbstractFactory;
import com.epam.factories.AirplaneFactory;
import com.epam.factories.HelicopterFactory;

public class Airlines {
	private List<Aircraft> depot = new ArrayList<Aircraft>();
	private static int numPassTotal;
	private static float tonnageTotal;
	private AbstractFactory airplaneFactory = new AirplaneFactory();
	private AbstractFactory helicopterFactory = new HelicopterFactory();

	public void purchaseRandomCraft() {
		int idPostfix = depot.size()+1;
		Random rand = new Random();
		if (rand.nextInt(2)==0)
			depot.add(airplaneFactory.createAircraft(idPostfix));
		else
			depot.add(helicopterFactory.createAircraft(idPostfix));
		Collections.sort(depot);
	}
	
	public static void incTotals(int numPass, float tonnage) {
		numPassTotal += numPass;
		tonnageTotal += tonnage;
	}

	public void launchAirplane(int numOfLaunches) {
		if (depot.isEmpty() == false) {
			Random rand = new Random();
			int depotSize = depot.size();
			for (int i=0; i<numOfLaunches; i++) {
				depot.get(rand.nextInt(depotSize)).makeFlight();				
			}
		}
	}

	public String findByConsumption(int min, int max) {
		String res = "\n";
		for (Aircraft airplane : depot) {
			if (airplane.getConsumption() >= min
					&& airplane.getConsumption() <= max)
				res += airplane.getName() + " consumes "
						+ airplane.getConsumption() + " kg/h\n";
		}
		return res;
	}

	public String listAllAirplanes() {
		String res = "";
		for (Aircraft airplane : depot) {
			res += airplane.toString() + "\n";
		}
		return res;
	}

	public String printTotals() {
		String res = "The total number of passengers: " + numPassTotal
				+ "\nThe total tonnage: " + tonnageTotal;
		return res;
	}

	public List<Aircraft> getDepot() {
		return depot;
	}

	public int getNumPassTotal() {
		return numPassTotal;
	}

	public float getTonnageTotal() {
		return tonnageTotal;
	}
}
